function CheckFrames(page) {
	if (parent.frames.length == 0) {
		window.location.href = page;
	}
}

function OverColor(obj, new_class, highlight_class) { 
	if(obj.className != highlight_class) {
		obj.className = new_class;
	}
}

function Highlight(obj, def_class, new_class) { 
	if(obj.className == def_class) { obj.className=new_class; }
	else                           { obj.className=def_class; }
}

// redirect to path // using for tr and onDblClick
function RecordToDo(path) {
	document.location.href = path;
}

function OverColorSimple(obj, old_class, new_class) { 
	obj.className = new_class;
}


// centered popup
function OpenWin(URL, winName, width, height, scroll, menubar, status) {

	var winLeft = (screen.width - width) / 2;
	var winTop = (screen.height - height) / 5;
	
	winData =  'height='+height+',width='+width+',top='+winTop+',left='+winLeft+',';
	winData += 'scrollbars='+scroll+',menubar'+menubar+',status='+status+'';
	//winData += 'alwaysRaised=1';
	win = window.open(URL, winName, winData);
	
	//alert(screen.width);
	//alert(winData);
	
	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

// we use it when patient pop up, to empty field
function doEmpty() {
	var field;
	for(i=0;i<arguments.length;i++){
		field = document.getElementById(arguments[i]);
		field.value = '';
	}
}

function ShowDiv(div, display_block) {
	var div = document.getElementById(div);
	var s = (display_block) ? 'block' : 'inline';
	div.style.display = s;
}

function HideDiv(div) {
	var div = document.getElementById(div);
	div.style.display = 'none';
}

function ShowHideDiv(div, display, display_block) {
	if(display) { ShowDiv(div, display_block); } 
	else        { HideDiv(div); } 
}

function ShowHelp(div, title, desc, width) {

	var width = (width) ? width : 170;
	var div = document.getElementById(div);
	var html = '<div>';
	html += (title) ? '<b>' + title + '</b><br>' : '';
	html += desc;
	html += '</div>';
	
	div.style.display = 'inline'; 
	div.style.position = 'absolute';
	div.style.width = width;
	div.style.backgroundColor = 'lightyellow';
	div.style.border = 'solid 1px black';
	div.style.padding = '3px';
	div.style.color = '#000000';
	div.innerHTML = html;
}

function HideHelp(div) {
	document.getElementById(div).style.display = 'none';
}

// set focus to obj, use true for second argument to set select to obj
function SetFocus(obj) {
	obj.focus();
	if(arguments[1]) { obj.select(); }
}

// show prompt to insert new value
function addOption(sel,oprompt) {
	
	var sel = document.getElementById(sel);	
	
	var other= sel.options[sel.selectedIndex];
	var newval= window.prompt(oprompt,'');
	if(!newval) { return; }
	sel.options[sel.options.length]= new Option(other.text,other.value,false,false);
	other.text = newval;
	other.value = 'new_'+newval;
}

function RowHighlightByCheckBox(row_id, ch_id, normal_class, highlight_class) { 
	
	var row = document.getElementById(row_id);
	var ch = document.getElementById(ch_id);
	
	if(ch.checked == true) {
		row.className = highlight_class;
	} else {
		row.className = normal_class;
	}
}

function RowHighlightOneOnly(id, normal_class, highlight_class) { 
	var rows = document.getElementsByTagName('tr');
	
	for(i=0;i<rows.length;i++){
		if(rows[i].id && rows[i].id == id) {
			rows[i].className = highlight_class;
		} else {
			if(rows[i].className != highlight_class) { continue; }
			rows[i].className = normal_class;
		}
	}
}



function parseGetVars() {
    var getVars = location.search.substring(1).split("&");
    var returnVars = new Array();
    
    for(i=0; i < getVars.length; i++) {
        var newVar = getVars[i].split("=");
        returnVars[unescape(newVar[0])] = unescape(newVar[1]);
    }
    
    return returnVars;
}


function selectAll(action, id, values) {
	
	var obj = document.getElementById(id);
	
	if(values != null) {
		var values = values.split(',');
		var apos_action = (action) ? false : true;
		
		selectAll(apos_action, id);
		
		for (i=0; i<values.length; i++) {
			for (j=0; j<obj.options.length; j++) {
				if(values[i] == obj.options[j].value) {
					obj.options[j].selected = action;
					break;
				}
			}
		}
		
	} else {
		for (i=0; i<obj.options.length; i++) {
			obj.options[i].selected = action;
		}	
	}
}


// assig more than one handker on page
function addHandler(object, event, handler)
{
  if (typeof object.addEventListener != 'undefined')
    object.addEventListener(event, handler, false);
  else if (typeof object.attachEvent != 'undefined')
    object.attachEvent('on' + event, handler);
  else
    throw "Incompatible browser";
}

function removeHandler(object, event, handler)
{
  if (typeof object.removeEventListener != 'undefined')
    object.removeEventListener(event, handler, false);
  else if (typeof object.detachEvent != 'undefined')
    object.detachEvent('on' + event, handler);
  else
    throw "Incompatible browser";
}


//Univesal onload
function setGlobalOnLoad(f) {
   var root = window.addEventListener || window.attachEvent ? window : document.addEventListener ? document : null
   if (root){
      if(root.addEventListener) root.addEventListener("load", f, false)
      else if(root.attachEvent) root.attachEvent("onload", f)
   } else {
      if(typeof window.onload == 'function') {
         var existing = window.onload
         window.onload = function() {
            existing()
            f()
         }
      } else {
         window.onload = f
      }
   }
}